﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using ENail.Domain.Core;
using ENail.Domain.Core.Contract.Services;
using ENail.Portal.Web.Models;
using Equation.Enterprise.Framework.Web.Model;

namespace ENail.Portal.Web.Controllers
{
    public class RegistrationController : DUALController<RegistrationListViewItem,
                                                       RegistrationViewModel,
                                                       Registration>
    {
        private readonly IRegistrationService _registrationService;
        private readonly ICustomerService _customerService;

        private readonly ViewModelMapper<ReceiptViewModel, Receipt> _receiptMapper;

        public RegistrationController(IRegistrationService registrationService,
                                    ICustomerService customerService)
            : base("RegistrationList",
                   "Registration",
                   new RegistrationListViewModelMapper(),
                   new RegistrationViewModelMapper(registrationService, customerService))
        {
            _registrationService = registrationService;
            _customerService = customerService;
            _receiptMapper = new ReceiptViewModelMapper();
        }

        [HttpGet]
        public ActionResult ViewReceipt(int id)
        {
            Receipt domainModel = GetReceipt(id);
            ReceiptViewModel viewModel = new ReceiptViewModel();
            _receiptMapper.MapDomainToView(domainModel, viewModel);

            return View("Receipt", viewModel);
        }

        [HttpPost]
        public ActionResult UpdateReceipt(int id, ReceiptViewModel model)
        {
            if (!ModelState.IsValid)
            {
                _receiptMapper.ReMap(model);
                return View("Receipt", model);
            }

            Receipt domainModel = GetReceipt(id);
            _receiptMapper.MapViewToDomain(model, domainModel);
            UpdateReceipt(domainModel);

            return RedirectToAction("ViewReceipt", new { id = id });
        }

        protected override List<Registration> GetList()
        {
            User user = User as User;
            return _registrationService.GetAllActiveRegistrations().Where(x => x.NailSalon.Id == user.NailSalon.Id).ToList();
        }

        protected override Registration Get(int id)
        {
            return _registrationService.GetRegistration(id);
        }

        protected override void Add(Registration domainModel)
        {
            User user = User as User;
            domainModel.NailSalon = user.NailSalon;

            _registrationService.InsertRegistration(domainModel);
        }

        protected override void Update(Registration domainModel)
        {
            _registrationService.UpdateRegistration(domainModel);
        }

        protected override void Delete(Registration domainModel)
        {
            _registrationService.DeActivateRegistration(domainModel);
        }

        private Receipt GetReceipt(int id)
        {
            Registration registration = Get(id);

            if (registration.Receipt == null)
                registration.Receipt = new Receipt { Registration = registration };

            registration.Receipt.updateReceipt();

            return registration.Receipt;
        }

        private void UpdateReceipt(Receipt domainModel)
        {
            _registrationService.UpdateRegistration(domainModel.Registration);
        }
    }
}